package com.example.mylibrary.data;

import android.content.Context;

import com.example.mylibrary.MyApp;
import com.example.mylibrary.greendao.ByteImgDao;
import com.example.mylibrary.greendao.DaoSession;
import com.example.mylibrary.greendao.ItemDao;
import com.example.mylibrary.greendao.ItemGropDao;
import com.example.mylibrary.greendao.ItemRollCallDao;
import com.example.mylibrary.greendao.ScheduleDao;
import com.example.mylibrary.greendao.SortDao;
import com.example.mylibrary.greendao.StudentInfoDao;
import com.example.mylibrary.greendao.StudentItemDao;
import com.example.mylibrary.greendao.StudentItemGropDao;
import com.example.mylibrary.greendao.StudentRollCallDao;

import org.greenrobot.greendao.database.Database;

import java.util.List;


/**
 * Created by Administrator on 2018/4/2.
 */

public class DBManager {
	private static DBManager mInstance;
	private static DaoSession daoSession;
	public static Database db;
	private static ByteImgDao byteImgDao;
	private static ItemDao itemDao;
	private static ItemGropDao itemGropDao;
	private static ItemRollCallDao itemRollCallDao;
	private static ScheduleDao scheduleDao;
	private static SortDao sortDao;
	private static StudentInfoDao studentInfoDao;
	private static StudentItemDao studentItemDao;
	private static StudentItemGropDao studentItemGropDao;
	private static StudentRollCallDao studentRollCallDao;

	public DBManager() {
	}

	public static DBManager getInstance(Context context) {
		if (mInstance == null) {
			synchronized (DBManager.class) {
				if (mInstance == null) {
					mInstance = new DBManager();
					daoSession = MyApp.getDaoSession(context);
					db = daoSession.getDatabase();

					byteImgDao = daoSession.getByteImgDao();
					itemDao = daoSession.getItemDao();
					itemGropDao = daoSession.getItemGropDao();
					itemRollCallDao = daoSession.getItemRollCallDao();
					scheduleDao = daoSession.getScheduleDao();
					sortDao = daoSession.getSortDao();
					studentInfoDao = daoSession.getStudentInfoDao();
					studentItemDao = daoSession.getStudentItemDao();
					studentItemGropDao = daoSession.getStudentItemGropDao();
					studentRollCallDao = daoSession.getStudentRollCallDao();
				}
			}
		}
		return mInstance;
	}

	public boolean insertItemSql(List<ItemGrop> itemGrops) {
		// String sql = "insert or ignore into"+;
		return true;
	}

	//查询单个身份证所属人信息
	public StudentInfo queryIdInfo(String idCard) {
		return MyApp.getDaoInstant().getStudentInfoDao().queryBuilder().where(StudentInfoDao.Properties.IdCard.eq(idCard)).unique();
	}

	//根据学号查询个人信息
	public StudentInfo queryStuInfo(String stuId) {
		return MyApp.getDaoInstant().getStudentInfoDao().queryBuilder().where(StudentInfoDao.Properties.StudenCode.eq(stuId)).unique();
	}

	public void insertitem(Item item) {
		itemDao.insertOrReplaceInTx(item);
	}

	/**
	 * 插入检录信息
	 *
	 * @param studentInfo
	 */
	public void insertResult(StudentInfo studentInfo) {
		studentInfoDao.updateInTx(studentInfo);
	}

	//查找所有项目id
	public List<Item> loadAllItems() {
		return itemDao.loadAll();
	}

	public List<StudentInfo> loadAllStudents() {
		return studentInfoDao.loadAll();
	}

	//根据项目id 查询StudentItem
	public StudentInfo queryStudentId(String stuCode) {
		return studentInfoDao.queryBuilder().where(StudentInfoDao.Properties.StudenCode.eq(stuCode)).unique();
	}

	//根据项目id
	public List<StudentItem> queryStudentItem(Long sid) {
		return studentItemDao.queryBuilder().where(StudentItemDao.Properties.StudentID.eq(sid)).list();
	}

	public List<Item> queryItemName(Long id) {
		return itemDao.queryBuilder().where(ItemDao.Properties.ItemID.eq(id)).list();
	}

	public List<Item> queryItemId(String itemName) {
		return itemDao.queryBuilder().where(ItemDao.Properties.ItemName.eq(itemName)).list();
	}

	public List<ItemGrop> itemGrop(Long itemId) {
		return itemGropDao.queryBuilder().where(ItemGropDao.Properties.ItemID.eq(itemId)).list();
	}

	public Schedule scheduleTime(Long scheduleId) {
		return scheduleDao.queryBuilder().where(ScheduleDao.Properties.ScheduleID.eq(scheduleId)).unique();
	}

	public List<Sort> sortAll() {
		return sortDao.loadAll();
	}

	public List<ItemGrop> queryItem(Long sortId) {
		return itemGropDao.queryBuilder().where(ItemGropDao.Properties.SortID.eq(sortId)).list();
	}

	//根据条件查询对应的分组信息
	public List<ItemGrop> queryGropId(long sortId, long itemId, int groupNo) {
		return itemGropDao.queryBuilder().where(ItemGropDao.Properties.SortID.eq(sortId))
				.where(ItemGropDao.Properties.ItemID.eq(itemId))
				.where(ItemGropDao.Properties.GropNo.eq(groupNo)).list();
	}

	public List<StudentItemGrop> queryStudentGrop(long itemGropId) {
		return studentItemGropDao.queryBuilder().where(StudentItemGropDao.Properties.ItemGroupID.eq(itemGropId)).list();
	}

	public List<StudentItem> queryStudentItemID(Long stuid) {
		return studentItemDao.queryBuilder().where(StudentItemDao.Properties.StudentItemID.eq(stuid)).list();
	}

	public List<StudentInfo> queryStuInfos(long stuId) {
		return studentInfoDao.queryBuilder().where(StudentInfoDao.Properties.StudentID.eq(stuId)).list();
	}

	/**
	 * 条件查询考生
	 *
	 * @param sex    性别
	 * @param record 检录状态
	 * @param send   上传状态
	 * @return
	 */
	public List<StudentInfo> queryStuInfosForSpinner(int sex, int record, int send) {
		List<StudentInfo> students = null;
		if (sex == 9999 && record == 9999 && send == 9999) {
			students = studentInfoDao.loadAll();
		} else if (sex == 9999 && record == 9999 && send != 9999) {
			students = studentInfoDao.queryBuilder().where(StudentInfoDao.Properties.UploadState.eq(send)).list();
		} else if (sex == 9999 && record != 9999 && send == 9999) {
			students = studentInfoDao.queryBuilder().where(StudentInfoDao.Properties.RollCallState.eq(record)).list();
		} else if (sex == 9999 && record != 9999 && send != 9999) {
			students = studentInfoDao.queryBuilder()
					.where(StudentInfoDao.Properties.RollCallState.eq(record))
					.where(StudentInfoDao.Properties.UploadState.eq(send))
					.list();
		} else if (sex != 9999 && record == 9999 && send == 9999) {
			students = studentInfoDao.queryBuilder().where(StudentInfoDao.Properties.Sex.eq(sex)).list();
		} else if (sex != 9999 && record == 9999 && send != 9999) {
			students = studentInfoDao.queryBuilder()
					.where(StudentInfoDao.Properties.Sex.eq(sex))
					.where(StudentInfoDao.Properties.UploadState.eq(send))
					.list();
		} else if (sex != 9999 && record != 9999 && send == 9999) {
			students = studentInfoDao.queryBuilder()
					.where(StudentInfoDao.Properties.Sex.eq(sex))
					.where(StudentInfoDao.Properties.RollCallState.eq(record))
					.list();
		} else if (sex != 9999 && record != 9999 && send != 9999) {
			students = studentInfoDao.queryBuilder()
					.where(StudentInfoDao.Properties.Sex.eq(sex))
					.where(StudentInfoDao.Properties.RollCallState.eq(record))
					.where(StudentInfoDao.Properties.UploadState.eq(send))
					.list();
		}
		return students;
	}

	public List<StudentInfo> queryStuInfos2(long sort) {
		return studentInfoDao.queryBuilder().where(StudentInfoDao.Properties.StudentID.eq(sort)).list();
	}

	public ByteImg queryStuImg(String studentId) {
		return byteImgDao.queryBuilder().where(ByteImgDao.Properties.StudentCode.eq(studentId)).unique();
	}

	public List<ByteImg> queryImg(String stuId) {
		return byteImgDao.queryBuilder().where(ByteImgDao.Properties.StudentCode.eq(stuId)).list();
	}

	/**
	 * 根据学号查询byteimg
	 */
	public ByteImg queryByteImg(String stuId) {
		return byteImgDao.queryBuilder().where(ByteImgDao.Properties.StudentCode.eq(stuId)).unique();
	}


}

